<?php

namespace app\admin\controller\collage;

use app\admin\model\GroupMaster;
use app\admin\model\User;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;

/**
 * @ControllerAnnotation(title="collage")
 */
class Collage extends AdminController
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\Collage();

    }

    public function memberIndex($id)
    {
        $master_id = $id;
        return view('collage\collage\bind_member', compact('master_id'));
    }

    public function collageMember($id)
    {
        $master_id = $id;
        return view('collage\collage\collage_member', compact('master_id'));
    }

    public function memberList($master_id)
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            if (empty(GroupMaster::find($master_id)->verification_id)){
                $collageInfo = $this->model->where('master_id', $master_id)->find();
                $user_id = explode(',',$collageInfo->user_id);
            }else{
                $verification_id = explode(',',GroupMaster::find($master_id)->verification_id);
                $collageInfo = $this->model->where('master_id', $master_id)->find();
                $user_id = explode(',',$collageInfo->user_id);
                $user_id = array_diff($verification_id,$user_id);
            }
            $userModel = new User();
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $userModel
                ->whereIn('id', $user_id)
                ->where($where)
                ->count();
            $list = $userModel
                ->whereIn('id', $user_id)
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            $data = [
                'code' => 0,
                'msg' => '',
                'count' => $count,
                'data' => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }

    public function collageMemberList($master_id)
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            if (!empty(GroupMaster::find($master_id)->verification_id)){
                $collageInfo = $this->model->where('master_id', $master_id)->find();
                if (!$collageInfo){
                    $user_id = explode(',',$collageInfo->user_id);
                }
            }else{
                $verification_id = explode(',',GroupMaster::find($master_id)->verification_id);
                $collageInfo = $this->model->where('master_id', $master_id)->find();
                $user_id = explode(',',$collageInfo->user_id);
                $user_id = array_diff($verification_id,$user_id);
            }
            $userModel = new User();
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $userModel
                ->whereIn('id', $user_id)
                ->where($where)
                ->count();
            $list = $userModel
                ->whereIn('id', $user_id)
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            $data = [
                'code' => 0,
                'msg' => '',
                'count' => $count,
                'data' => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }

    public function bindMember()
    {
        $params = input();
        $params['user_id'] = implode(',', $params['user_id']);
        $res = \app\admin\model\GroupMaster::where('id', $params['master_id'])->update(['verification_id' => $params['user_id']]);
        if (!$res) {
            return json(['code' => 0, 'msg' => '添加核销员失败', 'data' => []]);
        }
        return json(['code' => 1, 'msg' => '添加成功', 'data' => []]);
    }
}